<?php
/**
 * Created by PhpStorm.
 * User: 86151 - 姜伟
 * Date: 2021/6/10
 * Time: 20:46.
 */

namespace Xinyu\Gen\Commands;

use Xinyu\Gen\Core\Config\CommonConfig;
use Xinyu\Gen\Core\GenClient;
use Xinyu\Gen\Core\Libs\CommonClient;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

class CreateServiceCommand extends Command
{
    /**
     * --module 【模块层，取值admin、web】
     * --name 【模型名】
     * --search 【搜索参数】.
     */
    protected $signature = 's {--module=} {--name=} {--search=}';

    protected $description = '生成service脚本';

    public function handle()
    {
        if ('local' !== env('APP_ENV')) {
            $this->error('该环境不允许执行该脚本');
            return false;
        }
        $this->createService();
    }

    // 生成service脚本
    public function createService()
    {
        $res = GenClient::getInstance()->createService([
            'module' => Str::studly($this->option('module') ?? CommonConfig::MODULE_ADMIN),
            'name' => Str::studly(CommonClient::getInstance()->getName($this->option('name'))),
            'search' => $this->option('search') ?? '',
        ]);
        -1 === $res['code'] ? $this->error($res['msg']) : $this->info($res['msg']);
    }
}
